# -*- codeing = utf-8 -*-
# @Time: 2022/6/23 23:09
# @Author: Foxhuty
# @File: email_received_by_subject.py
# @Software: PyCharm
# @Based on python 3.10

from imbox import Imbox
import datetime
import os
import time
import shutil


def make_directory(f):
    def wrapper(*args, **kwargs):
        if not os.path.exists(f'D:\\数据\\语文'):
            os.makedirs(f'D:\\数据\\语文')
        if not os.path.exists(f'D:\\数据\\数学'):
            os.makedirs(f'D:\\数据\\数学')
        if not os.path.exists(f'D:\\数据\\英语'):
            os.makedirs(f'D:\\数据\\英语')
        if not os.path.exists(f'D:\\数据\\物理'):
            os.makedirs(f'D:\\数据\\物理')
        if not os.path.exists(f'D:\\数据\\化学'):
            os.makedirs(f'D:\\数据\\化学')
        if not os.path.exists(f'D:\\数据\\生物'):
            os.makedirs(f'D:\\数据\\生物')
        if not os.path.exists(f'D:\\数据\\政治'):
            os.makedirs(f'D:\\数据\\政治')
        if not os.path.exists(f'D:\\数据\\历史'):
            os.makedirs(f'D:\\数据\\历史')
        if not os.path.exists(f'D:\\数据\\地理'):
            os.makedirs(f'D:\\数据\\地理')
        if not os.path.exists(f'D:\\数据\\音乐'):
            os.makedirs(f'D:\\数据\\音乐')
        if not os.path.exists(f'D:\\数据\\体育'):
            os.makedirs(f'D:\\数据\\体育')
        if not os.path.exists(f'D:\\数据\\美术'):
            os.makedirs(f'D:\\数据\\美术')
        if not os.path.exists(f'D:\\数据\\未分类'):
            os.makedirs(f'D:\\数据\\未分类')
        result = f(*args, **kwargs)
        return result

    return wrapper


@make_directory
def get_mail_attachments(year, month, day):
    start = time.time()
    n = 0
    with Imbox(mail_host, username=sender, password=mail_pass, ssl=True) as imbox:
        all_inbox_messages = imbox.messages(unread=True)
        for uid, message in all_inbox_messages:
            try:
                mail_date = message.parsed_date.date()
                date_filter = datetime.date(year, month, day)
                if message.attachments:
                    if mail_date >= date_filter:
                        for attachment in message.attachments:
                            with open(f'D:\\数据\\未分类\\{attachment["filename"]}', mode='wb') as f:
                                f.write(attachment['content'].getvalue())
                                f.close()
                                imbox.mark_seen(uid)
                                n += 1
                                print(f'第{n}封邮件:{mail_date}---{message.subject}----{attachment["filename"]}---下载完成!')
                                if '语文' in message.subject:
                                    shutil.move(f'D:\\数据\\未分类\\{attachment["filename"]}', f'D:\\数据\\语文')
                                elif '数学' in message.subject:
                                    shutil.move(f'D:\\数据\\未分类\\{attachment["filename"]}', f'D:\\数据\\数学')
                                elif '英语' in message.subject:
                                    shutil.move(f'D:\\数据\\未分类\\{attachment["filename"]}', f'D:\\数据\\英语')
                                elif '物理' in message.subject:
                                    shutil.move(f'D:\\数据\\未分类\\{attachment["filename"]}', f'D:\\数据\\物理')
                                elif '化学' in message.subject:
                                    shutil.move(f'D:\\数据\\未分类\\{attachment["filename"]}', f'D:\\数据\\化学')
                                elif '生物' in message.subject:
                                    shutil.move(f'D:\\数据\\未分类\\{attachment["filename"]}', f'D:\\数据\\生物')
                                elif '政治' in message.subject:
                                    shutil.move(f'D:\\数据\\未分类\\{attachment["filename"]}', f'D:\\数据\\政治')
                                elif '历史' in message.subject:
                                    shutil.move(f'D:\\数据\\未分类\\{attachment["filename"]}', f'D:\\数据\\历史')
                                elif '地理' in message.subject:
                                    shutil.move(f'D:\\数据\\未分类\\{attachment["filename"]}', f'D:\\数据\\地理')
                                elif '体育' in message.subject:
                                    shutil.move(f'D:\\数据\\未分类\\{attachment["filename"]}', f'D:\\数据\\体育')
                                elif '音乐' in message.subject:
                                    shutil.move(f'D:\\数据\\未分类\\{attachment["filename"]}', f'D:\\数据\\音乐')
                                elif '美术' in message.subject:
                                    shutil.move(f'D:\\数据\\未分类\\{attachment["filename"]}', f'D:\\数据\\美术')
                    else:
                        print('日期不匹配')
            except:
                pass

        end = time.time()
        time_spent = round((end - start) / 60, 1)
        print(f'----------邮件接收完毕,共计用时{time_spent}分钟---------------')


if __name__ == '__main__':
    mail_host = 'imap.qq.com'
    # 设定接收邮箱许可证密码
    mail_pass = 'gdhprdriyijibjfi'
    # mail_pass = 'tensjeayhyelccbe'
    # 可修改接收邮箱email:
    # sender = '1801007676@qq.com'
    sender = 'foxhuty@qq.com'
    # 传入接收邮件的日期过滤条件：如要接收2020年1月1日及之后的所有邮件，在函数参数中传入2020，1，1
    get_mail_attachments(2022, 6, 30)
